package com.ft.oa.service.clueprojectService.Impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.ft.oa.domain.clueproject.ClueProjectProgressAttachment;
import com.ft.oa.domain.dto.knowledgeBase.AttachmentDto;
import com.ft.oa.enums.DirectoryTypeEnum;
import com.ft.oa.mapper.clueprojectMapper.ClueProjectProgressAttachmentMapper;
import com.ft.oa.service.clueprojectService.ClueprojectprogressattachmentServer;
import com.ft.oa.service.knowledgeBase.DirectoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class progressattachmentServerImpl implements ClueprojectprogressattachmentServer {
    @Resource
    private ClueProjectProgressAttachmentMapper clueProjectProgressAttachmentMapper;
    @Resource
    private DirectoryService directoryService;

    /**
     * 新增项目售前附件
     * @param clueProjectProgressAttachment
     */
    @Override
    public void addpresalefile(ClueProjectProgressAttachment clueProjectProgressAttachment) {
        List<AttachmentDto> attachments = clueProjectProgressAttachment.getAttachments();
        if(attachments != null && attachments.size() > 0){
            String json = JSONArray.toJSONString(attachments);
            clueProjectProgressAttachment.setAchment(json);
        }else{
            clueProjectProgressAttachment.setAchment(null);
        }
        clueProjectProgressAttachmentMapper.insert(clueProjectProgressAttachment);
        directoryService.saveProAttachment(attachments, DirectoryTypeEnum.PRO_FD_CLUE.getCode(),String.valueOf(clueProjectProgressAttachment.getId()),null,clueProjectProgressAttachment.getProjectnumber());
    }

    @Override
    public List<ClueProjectProgressAttachment> getpresalefile(int id) {
        QueryWrapper<ClueProjectProgressAttachment> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(ClueProjectProgressAttachment::getClueProjectProgressId,id);
        List<ClueProjectProgressAttachment> clueProjectProgressAttachments = clueProjectProgressAttachmentMapper.selectList(wrapper);
        clueProjectProgressAttachments.forEach(e->{
            String achment = e.getAchment();
            e.setAchment("");
            e.setAttachments(JSONArray.parseArray(achment,AttachmentDto.class));
        });
        return clueProjectProgressAttachments;
    }

    @Override
    public void updateStats(int id, int starts) {
        LambdaUpdateChainWrapper<ClueProjectProgressAttachment> wrapper = new LambdaUpdateChainWrapper<>(clueProjectProgressAttachmentMapper);
        wrapper.eq(ClueProjectProgressAttachment::getId,id)
                .set(ClueProjectProgressAttachment::getStarts,starts)
                .update();
    }

}
